using Sirenix.OdinInspector;
using UnityEngine;

namespace WorkFlow.Tools.UI
{
    [ExecuteAlways]
    [RequireComponent(typeof(RectTransform))]
    [RequireComponent(typeof(UIBehaviourDriver))]
    [AddComponentMenu("WorkFlow/UGUI/UIBehaviour/ElementFill")]
    public class ElementFill : MonoBehaviour,IUIBehaviourDriverElement
    {
        [SerializeField] int order;
        [SerializeField] FillType fillType;
        int IUIBehaviourDriverElement.Order => order;
        bool IUIBehaviourDriverElement.ElementEnabled  => enabled;
        
        private RectTransform _rectTransform;
        RectTransform URectTransform
        {
            get
            {
                if (_rectTransform == null)
                    _rectTransform = GetComponent<RectTransform>();
                return _rectTransform;
            }
        }
        private RectTransform _parentRectTransform;
        RectTransform UParentRectTransform
        {
            get
            {
                if (_parentRectTransform == null)
                    _parentRectTransform = URectTransform.parent.GetComponent<RectTransform>();
                return _parentRectTransform;
            }
        }
        
        public void OnRectTransformDimensionsChange()
        {
            if (!enabled) return;
                Apply();
        }

        [Button]
        void Apply()
        {
            if (UParentRectTransform != null)
            {
                if (fillType == FillType.FillParent)
                {
                    if (UParentRectTransform.rect.width > URectTransform.rect.width)
                    {
                        URectTransform.localScale = URectTransform.rect.width / UParentRectTransform.rect.width * Vector3.one;
                    }
                    else
                    {
                        URectTransform.localScale = UParentRectTransform.rect.width / URectTransform.rect.width * Vector3.one;
                    }
                    float factHight = URectTransform.rect.height * URectTransform.localScale.y;
                    if (factHight > UParentRectTransform.rect.height)
                    {
                        URectTransform.localScale = UParentRectTransform.rect.height / URectTransform.rect.height * Vector3.one;
                    }
                }
                else if (fillType == FillType.FullParent)
                {
                    if (UParentRectTransform.rect.width > URectTransform.rect.width)
                    {
                        URectTransform.localScale = UParentRectTransform.rect.width / URectTransform.rect.width * Vector3.one;
                    }
                    else
                    {
                        URectTransform.localScale = URectTransform.rect.width / UParentRectTransform.rect.width * Vector3.one;
                    }
                    float factHight = URectTransform.rect.height * URectTransform.localScale.y;
                    if (factHight < UParentRectTransform.rect.height)
                    {
                        URectTransform.localScale = UParentRectTransform.rect.height / URectTransform.rect.height * Vector3.one;
                    }
                }
                else
                {
                    URectTransform.localScale = Vector3.one;
                }
            }
        }
#if UNITY_EDITOR
        private bool _isValidate;
        bool IUIBehaviourDriverElement.IsValidated => _isValidate;
        void OnValidate()
        {
            _isValidate = true;
        }
#endif
    }
}
